<?php

namespace App\Repository\Repositories;

use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use App\Repository\Models\OrderProducts;

/**
 * Class OrderProductsRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class OrderProductsRepositoryEloquent extends BaseRepository implements OrderProductsRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'is_appraise',
    ];

    /**
     * 可关联查询的字段
     */
    public $withAllow = [
        'product',
        'sku'
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return OrderProducts::class;
    }

    public function batchCreate($order_products, $order_code)
    {
        foreach($order_products as $order_product){
            $order_product['order_code'] = $order_code;
            $this->create($order_product);
        }
    }

    public function batchUpdate($shop_id, $order_code, $order_products)
    {
        $update_count = 0;
        foreach($order_products as $order_product){
            if(array_get($order_product, 'order_code') != $order_code || array_get($order_product, 'shop_id') != $shop_id){
                //剔除非法数据，剔除商品中shop_id与order_code匹配不上的数据，跳过此次循环
                continue;
            }
            $this->query()->update($order_product);
            $update_count += 1;
        }

        //无更新则返回false
        return $update_count > 0 ? true : false;
    }

}
